package rabbitmq;

import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

/**
 * 消息消费者
 */
public class Customer2 {


	/**
	 * 轮询方式，get
	 */
	@Test
	public void consumerTest() {
		Connection connection = null;
		Channel channel = null;
		try {
			ConnectionService conn = new ConnectionService();
			connection = conn.initConn();
			channel = connection.createChannel();

			//轮询poll API方式
			GetResponse response = channel.basicGet("firstQueue", false);
			//一次取一条
			String message = new String(response.getBody(), "UTF-8");
			System.out.println(message);
//			while (true) {
//				String message = new String(response.getBody(), "UTF-8");
//				System.out.println(message);
//			}
			//应答删除
			channel.basicAck(response.getEnvelope().getDeliveryTag(), false);

		} catch(Exception ex) {
			ex.printStackTrace();
		}
	}
}
